package com.song.controller.menu;

import com.song.common.Result;
import com.song.controller.BaseController;
import com.song.entity.menu.Menu;
import com.song.entity.menu.RoleMenu;
import com.song.service.menu.MenuService;
import com.song.service.menu.RoleMenuService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("${api.version}/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     *  菜单管理界面
     */
    @RequestMapping("/menuManage")
    public String menuManage() {
        return "menu/menuManage";
    }

    /**
     * 获取菜单列表
     */
    @RequestMapping("/menuList")
    @ResponseBody
    public Result<List<Menu>> menuList(Menu menu, String startTime, String endTime,
                               @RequestParam(value = "page", defaultValue = "0") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Result<List<Menu>> result  =  menuService.selectMenuList(menu,startTime,endTime,pageNum,pageSize);
        return result;
    }

    /**
     *  根据用户ID删除菜单
     */
    @RequestMapping("/deleteMenuById")
    @ResponseBody
    public Result deleteMenuById(int menuId) {
         boolean bool = menuService.deleteById(menuId);
         if (bool) {
            return success("删除菜单成功！");
         } else {
             return error("删除菜单失败！");
         }
    }

    /**
     * 批量删除菜单
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Result<Integer> batchDel(String ids) {
        String[] array = ids.split(",");
        boolean bool =  menuService.deleteBatchIds(Arrays.asList(array));
        if (bool) {
            return success("批量删除菜单成功！");
        } else {
            return error("批量删除菜单失败！");
        }
    }

    /**
     * 新增菜单
     */
    @RequestMapping("/addMenu")
    @ResponseBody
    public Result<Integer> addMenu(Menu menu) {

        menu.setCreateTime(new Date());
        menu.setCreateAdmin("admin");
        boolean bool = menuService.insert(menu);
        if (bool) {
            return success("新增菜单成功！");
        } else {
            return error("新增菜单失败！");
        }
    }

    /**
     * 更新菜单
     */
    @RequestMapping("/updateMenu")
    @ResponseBody
    public Result<Integer> updateMenu(Menu menu) {
        boolean bool = menuService.updateById(menu);
        if (bool) {
            return success("更新菜单成功！");
        } else {
            return error("更新菜单失败！");
        }
    }

    /**
     * 根据角色ID,获取菜单树
     */
    @RequestMapping("/getMenuTreeByRoleId")
    @ResponseBody
    public JSONArray getMenuTreeByRoleId(Integer roleId) {
        return menuService.getMenuTreeByRoleId(roleId);
    }

    /**
     * 新增角色所属菜单列表
     */
    @RequestMapping("/addRoleMenus")
    @ResponseBody
    public Result addRoleMenus(Integer roleId, String menuIds) {
        String[] ids = menuIds.split(",");
        boolean bool = roleMenuService.deleteById(roleId);
        if (!bool) {
            return error("删除角色对应的菜单列表失败！");
        }
        List<RoleMenu> list = new ArrayList<>();
        // 至少有一条数据
        if(!ids[0].equals("")) {
            for (String menuId : ids) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(Integer.valueOf(menuId));
                list.add(roleMenu);
            }
            roleMenuService.batAddRoleMenus(list);
        }
        return success();
    }

}
